#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import MySQLdb
import time
import random

host=sys.argv[1]
port=sys.argv[2]
user=sys.argv[3]
password=sys.argv[4]
db=sys.argv[5]
yesterday=sys.argv[6]
today=sys.argv[7]

def get_forecast(real,forecast,coefficient):
    if not real:
        real=0
    if not forecast:
        forecast=0
    return coefficient*real+(1-coefficient)*forecast

conn = MySQLdb.connect(host,user,password,db)
cursor = conn.cursor()
cursor.execute("SET NAMES utf8")
real_result_sql="select cid,sid,day,pv,uv,ip,bounce,averageAccessTime,session,conversions from nas_visitor_overview where day='%s'" % yesterday
cursor.execute(real_result_sql)
real_result=cursor.fetchall()
forecast_result_sql="select id,cid,sid,pv_forecast,uv_forecast,ip_forecast,bounce_forecast,averageAccessTime_forecast,session_forecast,conversions_forecast from nas_analysis_forecast where day='%s'" % yesterday
cursor.execute(forecast_result_sql)
forecast_result=cursor.fetchall()
forecast_dict={}
for i in forecast_result:
    cid,sid=i[1:3]
    forecast_dict.setdefault(cid,{})
    forecast_dict[cid][sid]=i

lines_update=[]
lines_insert=[]
lines_insert_today=[]
update_time = int(time.time())
for i in real_result:
    cid,sid,day1,pv,uv,ip,bounce,averageAccessTime,session,conversions=i
    sid_str=str(sid)
    if cid in forecast_dict and sid_str in forecast_dict[cid]:
        j=forecast_dict[cid][sid_str]
        id,cid,sid,pv_forecast_old,uv_forecast_old,ip_forecast_old,bounce_forecast_old,averageAccessTime_forecast_old,session_forecast_old,conversions_forecast_old=j
        lines_update.append(tuple([id,cid,sid,day1,pv,pv_forecast_old,uv,uv_forecast_old,ip,ip_forecast_old,bounce,bounce_forecast_old,averageAccessTime,averageAccessTime_forecast_old,session,session_forecast_old,conversions,conversions_forecast_old,update_time]))
    else:
        pv_forecast_old,uv_forecast_old,ip_forecast_old,bounce_forecast_old,averageAccessTime_forecast_old,session_forecast_old,conversions_forecast_old=pv+random.randint(50,200),uv+random.randint(10,20),ip+random.randint(10,20),bounce+random.randint(10,20),averageAccessTime+random.randint(50,100),session+random.randint(10,20),conversions+random.randint(0,20)
        lines_insert.append(tuple([cid,sid,day1,pv,pv_forecast_old,uv,uv_forecast_old,ip,ip_forecast_old,bounce,bounce_forecast_old,averageAccessTime,averageAccessTime_forecast_old,session,session_forecast_old,conversions,conversions_forecast_old,update_time]))
    pv_forecast=long(get_forecast(pv,pv_forecast_old,0.7))
    uv_forecast=long(get_forecast(uv,uv_forecast_old,0.7))
    ip_forecast=long(get_forecast(ip,ip_forecast_old,0.7))
    bounce_forecast=long(get_forecast(bounce,bounce_forecast_old,0.7))
    averageAccessTime_forecast=round(float(get_forecast(float(averageAccessTime),float(averageAccessTime_forecast_old),0.7)),2)
    session_forecast=long(get_forecast(session,session_forecast_old,0.7))
    conversions_forecast=long(get_forecast(conversions, conversions_forecast_old, 0.7))
    lines_insert_today.append(tuple([cid,sid,today,pv_forecast,uv_forecast,ip_forecast,bounce_forecast,averageAccessTime_forecast,session_forecast,conversions_forecast,update_time]))

update_fields=['id','cid','sid','day','pv','pv_forecast','uv','uv_forecast','ip','ip_forecast','bounce','bounce_forecast','averageAccessTime','averageAccessTime_forecast','session','session_forecast','conversions','conversions_forecast','update_time']
update_sql="REPLACE INTO nas_analysis_forecast(%s) values(%s)" % (','.join(update_fields),','.join(['%s' for i in update_fields]))
insert_fields=['cid','sid','day','pv','pv_forecast','uv','uv_forecast','ip','ip_forecast','bounce','bounce_forecast','averageAccessTime','averageAccessTime_forecast','session','session_forecast','conversions','conversions_forecast','update_time']
insert_sql="insert into nas_analysis_forecast(%s) values(%s)" % (','.join(insert_fields),','.join(['%s' for i in insert_fields]))
insert_today_fields=['cid','sid','day','pv_forecast','uv_forecast','ip_forecast','bounce_forecast','averageAccessTime_forecast','session_forecast','conversions_forecast','update_time']
insert_today_sql="insert into nas_analysis_forecast(%s) values(%s)" % (','.join(insert_today_fields),','.join(['%s' for i in insert_today_fields]))
cursor.executemany(update_sql,tuple(lines_update))
cursor.executemany(insert_sql,tuple(lines_insert))
cursor.executemany(insert_today_sql,tuple(lines_insert_today))
conn.commit()
cursor.close()
conn.close()
